# -*-coding:utf-8 -*-

"""
# File       : redisForApi.py
# Time       ：2022/9/9 上午7:08
# Author     ：puppet
# version    ：python 3.8.1
# Description：
"""
import redis
from redis import Redis
from rentalsite.settings import reidsPort, CurrentServerIp
import json, time

#
# myredis = Redis(host=CurrentServerIp, port=reidsPort)
# myredis.set('1', '333', ex=10)
# # myredis.delete('111')
# myredis.set('kkk', "555")
# print('sss', myredis.get('kkk'))


class RedisUtilClass(Redis):
    def __init__(self):
        super().__init__()
        self.pool = redis.ConnectionPool(host=CurrentServerIp, port=reidsPort)
        self.myredis = Redis(connection_pool=self.pool)

    def setRedis(self, key, value, ex=None):
        self.myredis.set(key, str(value), ex=ex)
    #
    # def getRedis(self, key):
    #     self.myredis.get(str(key))

    def deleteRedis(self, key):
        self.myredis.delete(str(key))

    def expireTimeLeft(self, key):
        timeleft = self.myredis.ttl(str(key))
        return timeleft


# if __name__ == '__main__':
#     value = {
#         'a': 1,
#         'b': 200
#     }
#     a = RedisUtilClass()
#     # a.delete('1')
#     # a.setRedis('ifhas', 'Y')
#     a.set('set', '10S', ex=30)
#     a.setRedis('setRedis', '10S', ex=30)
#     print(a.get('time'))
#     # time.sleep(2)
#     # a.expireTimeLeft('name')
#     # a.setRedis('dicts', json.dumps(value))
